<?php
///关于标基本标的统计信息
if(!isset($dbselect)) //前面没设置 数据库 则指定使用 DB3
    $dbselect   = Db::G3();
$basewhere      = "";
//坏账率时限
$bad_debt=cfg::get('bad_debt',90);
//逾期率时限
$overdue=cfg::get('overdue',30);
if($startdate!="" && $enddate!=""){
  $basewhere        = " and reverify_time>='$startdate' and  reverify_time<='$enddate'";
  $repaywhere_bad   =" and repay_backtime<='$enddate' and late_days>$bad_debt";
  $repaywhere_over  =" and repay_backtime<='$enddate' and late_days>$overdue";
  $recoverwhere     ="and f.time_h>='$startdate' and  f.time_h<='$enddate'";
}
//分站
$rows = $dbselect->GetRows("SELECT b.cid,SUBSTR(f.time_h,1,10) as reverify_time, count(distinct b.bid) c_acount,SUM(f.account) v_acount, 
SUM(recover_account_interest) v_account_i
FROM 05b_1tenderfinal f 
left join 05b_0base b on 
f.bid=b.bid 
WHERE b.flag=8 and f.status=1 and per=1 and f.tid=0 $recoverwhere
GROUP BY b.cid , SUBSTR( f.time_h, 1, 10 )");
foreach($rows as &$row)
{   
    $cid    = $row['cid'];
    $count  = $row['c_acount'];
    $ctime  = $row['reverify_time'];
    if($row['v_acount']*1<>0){
        CTLog::G()->pxxDayReal($count.",cid",$cid,"借款总额_流转",$row['v_acount'],$ctime); //分站的借款总额 
    }
    if($row['v_account_i']*1<>0){
         CTLog::G()->pxxDayReal($count.",cid",$cid,"总借款利息_流转",$row['v_account_i'],$ctime); //该还利息
    }
}
//分站
$rows = $dbselect->GetRows("SELECT cid,SUBSTR(reverify_time,1,10) as reverify_time, count(account) c_acount,SUM( account) v_acount, 
SUM(repay_account_interest) v_account_i,sum(repay_account_other) v_account_o, SUM(account_diya) v_account_d,
SUM(borrow_jiangli) v_jiangli, sum(IF( borrow_jiangli >0, 1, 0 )) AS c_jiangli,
sum(if(bnum=1,1,0)) c_new,sum(if(bnum=1,account,0)) c_newaccount,
sum(if(is_xudai=1,1,0)) c_xudai,sum(if(is_xudai=1,account,0)) c_xudaiaccount,
max(account) as maxaccount,min(account) as minaccount
FROM 05b_0base
WHERE STATUS IN ( 3, 6 ) and flag<>8  $basewhere
GROUP BY cid , SUBSTR( reverify_time, 1, 10 )");
foreach($rows as &$row)
{   
    $cid    = $row['cid'];
    $count  = $row['c_acount'];
    $ctime  = $row['reverify_time'];
    $c_jl   = $row['c_jiangli'];
    $c_new  = $row['c_new'];
    $c_xudai= $row['c_xudai'];
    //标复审后
    if($row['v_acount']*1<>0){
        CTLog::G()->pxxDayReal($count.",cid",$cid,"借款总额",$row['v_acount'],$ctime); //分站的借款总额 
    }
    if($row['v_account_i']*1<>0){
         CTLog::G()->pxxDayReal($count.",cid",$cid,"总借款利息",$row['v_account_i'],$ctime); //该还利息
    }
    if($row['v_account_o']*1<>0){
        CTLog::G()->pxxDayReal($count.",cid",$cid,"总借款费用",$row['v_account_o'],$ctime);    //总计  其他费费
    }
    if($row['v_account_d']*1<>0){
        CTLog::G()->pxxDayReal($count.",cid",$cid,"总借款押金",$row['v_account_d'],$ctime);    //押金       
    }
    if($row['v_jiangli']*1<>0){                  
        CTLog::G()->pxxDayReal($c_jl.",cid",$cid,"借款奖励",$row['v_jiangli'],$ctime);//分站统计 奖励金额
    }
    if($row['c_newaccount']*1<>0){                    
        CTLog::G()->pxxDayReal($c_new.",cid",$cid,"新增借款总额",$row['c_newaccount'],$ctime);//分站统计 奖励金额
    }
    if($row['c_xudaiaccount']*1<>0){                    
        CTLog::G()->pxxDayReal($c_xudai.",cid",$cid,"续贷借款总额",$row['c_xudaiaccount'],$ctime);//分站统计 奖励金额
    }
    if($row['maxaccount']*1<>0){                    
        CTLog::G()->pxxDayReal("cid",$cid,"最大标额",$row['maxaccount'],$ctime);//分站统计 奖励金额
    }
    if($row['minaccount']*1<>0){                   
        CTLog::G()->pxxDayReal("cid",$cid,"最小标额",$row['minaccount'],$ctime);//分站统计 奖励金额
    }
}


$rows = $dbselect->GetRows("SELECT cid,borrow_period,SUBSTR(reverify_time,1,10) as reverify_time, 
count(account) c_acount,SUM( account) v_acount
FROM 05b_0base
WHERE STATUS IN ( 3, 6 ) and days=0   $basewhere
GROUP BY cid ,SUBSTR(reverify_time, 1, 10),borrow_period");
foreach($rows as &$row)
{   
    $cid    = $row['cid'];
    $count  = $row['c_acount'];
    $ctime  = $row['reverify_time'];
    $period = $row['borrow_period'];
    if($row['v_acount']*1<>0){
        CTLog::G()->pxxDayReal($count.",cid",$cid,"借款期限_$period",$row['v_acount'],$ctime); 
    }
}

$rows = $dbselect->GetRows("SELECT cid,SUBSTR(reverify_time,1,10) as reverify_time, 
count(account) c_acount,SUM( account) v_acount
FROM 05b_0base
WHERE STATUS IN ( 3, 6 ) and days>0   $basewhere
GROUP BY cid ,SUBSTR(reverify_time, 1, 10)");
foreach($rows as &$row)
{   
    $cid    = $row['cid'];
    $count  = $row['c_acount'];
    $ctime  = $row['reverify_time'];
    if($row['v_acount']*1<>0){
        CTLog::G()->pxxDayReal($count.",cid",$cid,"借款期限_天",$row['v_acount'],$ctime); //分站的借款总额 
    }
}


$rows = $dbselect->GetRows("SELECT cid,SUM(r.dj_bzj) AS dj_bzj,SUM(IF(r.dj_bzj>0,1,0)) AS bzjcount,
SUM(r.dj_sq) dj_sq,SUM(IF(r.dj_sq>0,1,0)) AS sqcount
FROM 05b_0base_run r LEFT JOIN 05b_0base b ON r.bid=b.bid
GROUP BY cid ");
foreach($rows as &$row)
{   
    $cid    = $row['cid'];
    $bzjc   = $row['bzjcount'];
    $dj_bzj = $row['dj_bzj'];
    $sqc    = $row['sqcount'];
    $dj_sq  = $row['dj_sq'];
    if($dj_bzj*1<>0){
        CTLog::G()->pxxDayReal($bzjc.",cid",$cid,"标_剩余保证金",$dj_bzj,$curday);  
    }
    if($dj_sq*1<>0){
        CTLog::G()->pxxDayReal($sqc.",cid",$cid,"标_剩余首期冻结",$dj_sq,$curday);  
    }
}



//总站
$rows = $dbselect->GetRows("SELECT SUBSTR(f.time_h,1,10) as reverify_time, count(b.account) c_acount,SUM(f.account) v_acount, 
SUM(recover_account_interest) v_account_i
FROM 05b_1tenderfinal f 
left join 05b_0base b on 
f.bid=b.bid 
WHERE b.flag=8 and f.status=1 and per=1 and f.tid=0 $recoverwhere
GROUP BY SUBSTR( f.time_h, 1, 10 )");
foreach($rows as &$row)
{   
    $count  = $row['c_acount'];
    $ctime  = $row['reverify_time'];
    if($row['v_acount']*1<>0){
        CTLog::G()->pxxDayReal($count.",sid",1,"借款总额_流转",$row['v_acount'],$ctime); //分站的借款总额 
    }
    if($row['v_account_i']*1<>0){
         CTLog::G()->pxxDayReal($count.",sid",1,"总借款利息_流转",$row['v_account_i'],$ctime); //该还利息
    }
}

$rows = $dbselect->GetRows("SELECT SUBSTR(reverify_time,1,10) as reverify_time, count(account) c_acount,SUM( account) v_acount, 
SUM(repay_account_interest) v_account_i,sum(repay_account_other) v_account_o, SUM(account_diya) v_account_d,
SUM(borrow_jiangli) v_jiangli, sum(IF( borrow_jiangli >0, 1, 0 )) AS c_jiangli,
sum(if(bnum=1,1,0)) c_new,sum(if(bnum=1,account,0)) c_newaccount,
sum(if(is_xudai=1,1,0)) c_xudai,sum(if(is_xudai=1,account,0)) c_xudaiaccount,
max(account) as maxaccount,min(account) as minaccount
FROM 05b_0base
WHERE STATUS IN ( 3, 6 ) and flag<>8 $basewhere
GROUP BY SUBSTR( reverify_time, 1, 10 )");
foreach($rows as &$row)
{   
    $count  = $row['c_acount'];
    $ctime  = $row['reverify_time'];
    $c_jl   = $row['c_jiangli'];
    $c_new  = $row['c_new'];
    $c_xudai= $row['c_xudai'];
    //标复审后
    if($row['v_acount']*1<>0){
        CTLog::G()->pxxDayReal($count.",sid",1,"借款总额",$row['v_acount'],$ctime); //分站的借款总额 
    }
    if($row['v_account_i']*1<>0){
         CTLog::G()->pxxDayReal($count.",sid",1,"总借款利息",$row['v_account_i'],$ctime); //该还利息
    }
    if($row['v_account_o']*1<>0){
        CTLog::G()->pxxDayReal($count.",sid",1,"总借款费用",$row['v_account_o'],$ctime);    //总计  其他费费
    }
    if($row['v_account_d']*1<>0){
        CTLog::G()->pxxDayReal($count.",sid",1,"总借款押金",$row['v_account_d'],$ctime);    //押金       
    }
    if($row['v_jiangli']*1<>0){                      
        CTLog::G()->pxxDayReal($c_jl.",sid",1,"借款奖励",$row['v_jiangli'],$ctime);//分站统计 奖励金额
    }
    if($row['c_newaccount']*1<>0){                        
        CTLog::G()->pxxDayReal($c_new.",sid",1,"新增借款总额",$row['c_newaccount'],$ctime);//分站统计 奖励金额
    }
    if($row['c_xudaiaccount']*1<>0){                    
        CTLog::G()->pxxDayReal($c_xudai.",sid",1,"续贷借款总额",$row['c_xudaiaccount'],$ctime);//分站统计 奖励金额
    }
    if($row['maxaccount']*1<>0){                       
        CTLog::G()->pxxDayReal("sid",1,"最大标额",$row['maxaccount'],$ctime);//分站统计 奖励金额
    }
    if($row['minaccount']*1<>0){                        
        CTLog::G()->pxxDayReal("sid",1,"最小标额",$row['minaccount'],$ctime);//分站统计 奖励金额
    }
}


$rows = $dbselect->GetRows("SELECT borrow_period,SUBSTR(reverify_time,1,10) as reverify_time, 
count(account) c_acount,SUM( account) v_acount
FROM 05b_0base
WHERE STATUS IN ( 3, 6 ) and days=0   $basewhere
GROUP BY SUBSTR(reverify_time, 1, 10),borrow_period");
foreach($rows as &$row)
{   
    $count  = $row['c_acount'];
    $ctime  = $row['reverify_time'];
    $period = $row['borrow_period'];
    if($row['v_acount']*1<>0){
        CTLog::G()->pxxDayReal($count.",sid",1,"借款期限_$period",$row['v_acount'],$ctime); 
    }
}

$rows = $dbselect->GetRows("SELECT SUBSTR(reverify_time,1,10) as reverify_time, 
count(account) c_acount,SUM( account) v_acount
FROM 05b_0base
WHERE STATUS IN ( 3, 6 ) and days>0   $basewhere
GROUP BY SUBSTR(reverify_time, 1, 10)");
foreach($rows as &$row)
{   
    $count  = $row['c_acount'];
    $ctime  = $row['reverify_time'];
    if($row['v_acount']*1<>0){
        CTLog::G()->pxxDayReal($count.",sid",1,"借款期限_天",$row['v_acount'],$ctime); //分站的借款总额 
    }
}



$rows = $dbselect->GetRows("SELECT SUM(r.dj_bzj) AS dj_bzj,SUM(IF(r.dj_bzj>0,1,0)) AS bzjcount,
SUM(r.dj_sq) dj_sq,SUM(IF(r.dj_sq>0,1,0)) AS sqcount
FROM 05b_0base_run r  ");
foreach($rows as &$row)
{   
    $bzjc   = $row['bzjcount'];
    $dj_bzj = $row['dj_bzj'];
    $sqc    = $row['sqcount'];
    $dj_sq  = $row['dj_sq'];
    if($dj_bzj*1<>0){
        CTLog::G()->pxxDayReal($bzjc.",sid",1,"标_剩余保证金",$dj_bzj,$curday);  
    }
    if($dj_sq*1<>0){
        CTLog::G()->pxxDayReal($sqc.",sid",1,"标_剩余首期冻结",$dj_sq,$curday);  
    }
}


$rows = $dbselect->GetRows("SELECT
SUM(repay_account) acount
FROM 05b_2list_repay
WHERE 1=1 $repaywhere_bad");
//已完成的标总金额
$tot_money=$dbselect->GetNum("SELECT SUM(account) FROM 05b_0base WHERE STATUS IN ( 3, 6 ) and reverify_time<=?",array($enddate));
foreach($rows as &$row)
{
    $ctime  = $curday;
    $bad=$row['acount']/$tot_money;
    if($bad*1>0){
        CTLog::G()->pxxDayReal("sid",1,"坏账率",$bad,$ctime); //坏账率
    }
}




$rows = $dbselect->GetRows("SELECT  
COUNT(repay_account) ccount
FROM 05b_2list_repay
WHERE 1=1 $repaywhere_over");
//已完成的标总数
$count=$dbselect->GetNum("SELECT COUNT(account) FROM 05b_0base WHERE STATUS IN ( 3, 6 ) and reverify_time<=?",array($enddate));
foreach($rows as &$row)
{
    $ctime  = $curday;
    $over=$row['ccount']/$count;
    if($bad*1>0){
        CTLog::G()->pxxDayReal("sid",1,"逾期率",$over,$ctime); //逾期率
    }
}

//代偿金额=垫支+垫付
$rows = $dbselect->GetRows("SELECT sum(dianfu_money)+sum(dianzi_money) as '代偿金额',COUNT(*) AS '次数'
FROM 05b_2list_repay
WHERE  (repay_backstate=0 or repay_backtime<='$enddate')");
foreach($rows as &$row)
{
    $ctime  = $curday;
    $count  = $row['次数'];
    $Money  = $row['代偿金额'];
    if($count>0){
        CTLog::G()->pxxDayReal($count.",sid",1,"代偿金额",$Money,$ctime);    //分站垫付收回统计
    }
}




$rows = $dbselect->GetRows("SELECT cid,SUBSTR(reverify_time,1,10) as reverify_time, count(account) c_acount,SUM( account) v_acount, 
SUM(repay_account_interest) v_account_i,sum(repay_account_other) v_account_o, SUM(account_diya) v_account_d,
SUM(borrow_jiangli) v_jiangli, sum(IF( borrow_jiangli >0, 1, 0 )) AS c_jiangli,
sum(if(bnum=1,1,0)) c_new,sum(if(bnum=1,account,0)) c_newaccount,
sum(if(is_xudai=1,1,0)) c_xudai,sum(if(is_xudai=1,account,0)) c_xudaiaccount,
max(account) as maxaccount,min(account) as minaccount
FROM 05b_0base
WHERE STATUS IN ( 3, 6 ) and flag=5  $basewhere
GROUP BY cid , SUBSTR( reverify_time, 1, 10 )");
foreach($rows as &$row)
{   
    $cid    = $row['cid'];
    $count  = $row['c_acount'];
    $ctime  = $row['reverify_time'];
    $c_jl   = $row['c_jiangli'];
    $c_new  = $row['c_new'];
    $c_xudai= $row['c_xudai'];
    //标复审后
    if($row['v_acount']*1<>0){
        CTLog::G()->pxxDayReal($count.",cid",$cid,"体验标_借款总额",$row['v_acount'],$ctime); //分站的借款总额 
    }
    if($row['v_account_i']*1<>0){
         CTLog::G()->pxxDayReal($count.",cid",$cid,"体验标_总借款利息",$row['v_account_i'],$ctime); //该还利息
    }
}

$rows = $dbselect->GetRows("SELECT cid,SUBSTR(reverify_time,1,10) as reverify_time, count(account) c_acount,SUM( account) v_acount, 
SUM(repay_account_interest) v_account_i,sum(repay_account_other) v_account_o, SUM(account_diya) v_account_d,
SUM(borrow_jiangli) v_jiangli, sum(IF( borrow_jiangli >0, 1, 0 )) AS c_jiangli,
sum(if(bnum=1,1,0)) c_new,sum(if(bnum=1,account,0)) c_newaccount,
sum(if(is_xudai=1,1,0)) c_xudai,sum(if(is_xudai=1,account,0)) c_xudaiaccount,
max(account) as maxaccount,min(account) as minaccount
FROM 05b_0base
WHERE STATUS IN ( 3, 6 ) and flag=5  $basewhere
GROUP BY SUBSTR( reverify_time, 1, 10 )");
foreach($rows as &$row)
{   
    $cid    = $row['cid'];
    $count  = $row['c_acount'];
    $ctime  = $row['reverify_time'];
    $c_jl   = $row['c_jiangli'];
    $c_new  = $row['c_new'];
    $c_xudai= $row['c_xudai'];
    //标复审后
    if($row['v_acount']*1<>0){
        CTLog::G()->pxxDayReal($count.",sid",1,"体验标_借款总额",$row['v_acount'],$ctime); //分站的借款总额 
    }
    if($row['v_account_i']*1<>0){
         CTLog::G()->pxxDayReal($count.",sid",1,"体验标_总借款利息",$row['v_account_i'],$ctime); //该还利息
    }
}



